<template>
	<view class="my-shop-add-wp">
		<view class="my-shop-add-form">
			<u--form errorType="border-bottom" labelPosition="top" labelWidth='180' labelAlign='left'
				:labelStyle="{fontSize: '26rpx',color: '#1D2129',height:'80rpx',lineHeight:'80rpx'}"
				:borderBottom="true">

				<u-form-item label="支付宝收款码" borderBottom class='formItemWp'>
					<view @click="choosePicImg(0)">
						<view v-if="cropperPicZfb">
							<u-image :src="cropperPicZfb" width="218rpx" height="218rpx" mode="widthFix"></u-image>
						</view>
						<view class="uploadWp" v-else>
							<u-icon name="plus" color="#c9cdd4" size="60"></u-icon>
							<text style="color: #c9cdd4;font-size: 24rpx;">点击上传</text>
						</view>

					</view>
				</u-form-item>
				<u-form-item label="微信收款码" borderBottom class='formItemWp'>
					<view @click="choosePicImg(1)">
						<view v-if="cropperPicWx">
							<u-image :src="cropperPicWx" width="218rpx" height="218rpx" mode="widthFix"></u-image>
						</view>
						<view class="uploadWp" v-else>
							<u-icon name="plus" color="#c9cdd4" size="60"></u-icon>
							<text style="color: #c9cdd4;font-size: 24rpx;">点击上传</text>
						</view>

					</view>
				</u-form-item>

				<u-form-item label="其他收款方式" borderBottom class='formItemWp'>
					<textarea v-model='other' @blur="bindTextAreaBlur" :maxlength="200"
						placeholder="请输入其他收款方式\n如收款人:xxx\n收款账号 :xxxxxxxxxxxxx\n收款银行：XXXXXXXXX"
						style="background:#fff;width: 100%; padding: 20rpx 0;font-size: 26rpx;line-height: 30rpx;" />
				</u-form-item>
			</u--form>
		</view>
		<view class="bottom-box">
			<u-button :throttleTime="800" @click="submit" :custom-style="{
				  background: '#276aee',
				  color: '#fff',
				  width: '682rpx',
				  border:'none'
				}">提交</u-button>
		</view>
		<ksp-cropper mode="fixed" :width="300" :height="300" :maxWidth="1024" :maxHeight="1024" :url="cropperUrl"
			@cancel="cropperUrl=''" @ok="cropperOk"></ksp-cropper>
	</view>


</template>

<script>
	import kspCropper from '@/components/ksp-cropper/ksp-cropper.vue'
	import {
		mapState,
		mapGetters
	} from 'vuex'
	import baseConfig from '@/config/baseUrl.js'
	export default {
		data() {
			return {
				cropperUrl: '',
				cropperPicZfb: '',
				cropperPicWx: '',
				imgIndex: 0,
				other: '',
				model: {

				},
				addPayInfo: false,
				fileListImgs: [], //详情图
				// rules: {
				// 	prodName: [{
				// 		required: true,
				// 		message: '请输入商品名称',
				// 		trigger: 'blur'
				// 	}],
				// },
			}
		},
		components: {
			kspCropper
		},
		onLoad(opt) {
			console.log(opt);
			this.payload = opt
			this.addPayInfo = opt.addPayInfo == 1 ? true : false
		},
		computed: {
			...mapGetters(['userInfo']),
		},
		methods: {

			bindTextAreaBlur(e) {
				// if (!e.detail.value) {
				// 	return uni.showToast({
				// 		title: '请输入商品简介',
				// 		icon: 'none'
				// 	});
				// }
				this.other = e.detail.value
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			choosePicImg(index) {

				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					success: (rst) => {
						// 设置url的值，显示控件
						this.imgIndex = index
						console.log(rst, 'rst');
						this.cropperUrl = rst.tempFiles[0].tempFilePath;

					}
				});
			},
			async cropperOk(ev) {
				this.cropperUrl = "";
				//上传
				if (this.imgIndex == 0) {
					this.cropperPicZfb = await this.uploadFilePromise(ev.path)
					console.log(this.cropperPicZfb, '裁剪-上传后路径-----zfb');
				} else {
					this.cropperPicWx = await this.uploadFilePromise(ev.path)
					console.log(this.cropperPicWx, '裁剪-上传后路径--wx');
				}

			},
			// 新增图片
			async afterRead(event) {
				console.log('新增图片', event);
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: event.name,
						url: result,
						fileName: event.file[0].type + event.file[0].size + '',
						filePath: result
					}))
					fileListLen++
				}

			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseConfig.uploadUrl + '/casugn-system/sys/common/upload',
						filePath: url,
						name: 'file',
						success: (res) => {
							let data = JSON.parse(res.data)
							if (data.success) {
								resolve(data.message)
							}
						}
					});
				})
			},
			async submit() {
				// let fileListImgs = this.fileListImgs.map(item => item.url)
				// fileListImgs = fileListImgs.join()
				if (!this.cropperPicZfb && !this.cropperPicWx && !this.other) {
					return uni.showToast({
						icon: 'none',
						title: '请填写至少一种收款方式'
					})
				}

				const {
					openId,
					id
				} = this.userInfo
				if (this.addPayInfo) {
					let payload = {
						aliPay: this.cropperPicZfb,
						openId: openId,
						rests: this.other,
						weChat: this.cropperPicWx
					}
					let res = await this.$api.addPayInfo(payload)
					if (res.success && res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: '添加成功'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)

					} else {
						uni.showToast({
							icon: 'none',
							title: res.message
						})
					}
				} else {
					//提交
				}


			},

		}
	}
</script>


<style lang="scss" scoped>
	textarea {

		word-wrap: break-word;
		white-space: pre-line;
	}

	::v-deep .u-upload .uicon-close {
		font-size: 24rpx !important;
		top: 6rpx !important;
	}

	.my-shop-add-wp {
		padding-bottom: 200rpx;

		.my-shop-add-form {
			margin: 32rpx;

			.uploadWp {
				width: 218rpx;
				height: 218rpx;
				background-color: #f7f8fa;
				justify-content: center;
				display: flex;
				flex-direction: column;
				align-items: center;
				text-align: center;
			}
		}


	}
</style>